package com.hksj.cm.service;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.base.Objects;
import com.hksj.cm.dto.DeviceListDTO;
import com.hksj.cm.mapper.DeviceListMapper;
import com.hksj.cm.vo.DeviceMetadataParseInfoVO;
import com.hksj.cm.vo.DeviceVO;
import com.hksj.cm.vo.DeviceMetadataHistoryVO;
import com.hksj.cm.vo.ParticleValueHistoryVO;
import com.hksj.common.core.common.SpecialityModelCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @ClassName DeviceListService
 * @Description TODO
 * @Author @陈章
 * @Date 2024/7/26 11:43
 * @Version 1.0
 */
@Service
public class DeviceListService {
    @Autowired
    private DeviceListMapper deviceListManager;

    public Page<DeviceVO> queryDeviceMetadataParseInfo(DeviceListDTO dto) {
        dto.setCode(SpecialityModelCodeEnum.cleanliness.getCode());
        Page<DeviceVO> page = new Page<>(dto.getPageNum(), dto.getPageSize());
        //根据条件分页查询所有设备
        List<DeviceVO> devices = deviceListManager.queryDeviceInfo(page, dto);
        // 使用 Stream API 提取所有 id 并收集到一个新的 List 中
        List<String> collect = devices.stream().map(DeviceVO::getId).collect(Collectors.toList());
        //查询当前设备对应的所有属性值
        List<DeviceMetadataParseInfoVO> vos = deviceListManager.queryDeviceMetadataParseInfo(collect);
        // 创建一个映射，以 DeviceVO 的 ID 作为键，DeviceVO 对象作为值
        Map<String, DeviceVO> deviceById = devices.stream()
                .collect(Collectors.toMap(DeviceVO::getId, device -> device));
        // 使用 Stream API 处理 DeviceMetadataParseInfoVO 集合并收集匹配的对象
        Map<String, List<DeviceMetadataParseInfoVO>> matchedMetadataByDeviceId = vos.stream()
                .filter(vo -> deviceById.containsKey(vo.getId()))
                .collect(Collectors.groupingBy(DeviceMetadataParseInfoVO::getId));
        // 遍历 DeviceVO 集合，并设置匹配的 DeviceMetadataParseInfoVO 列表
        matchedMetadataByDeviceId.forEach((deviceId, metadataList) -> {
            DeviceVO device = deviceById.get(deviceId);
            device.setDeviceListVOS(metadataList);
        });
        //设置禁止sql优化
        page.setOptimizeCountSql(false);
        page.setRecords(devices);
        return page;
    }

    public DeviceVO eqParticleValueHistory(String deviceId, int section) {
        //根据设备id查询设备所在区域名称
        DeviceVO device = deviceListManager.deviceLocation(deviceId);
        //根据产品id、设备id查询设备的历史数据
        Long productId = device.getProductId();
        String tableName = "properties_" + productId;
        List<DeviceMetadataHistoryVO> list = deviceListManager.eqParticleValueHistoryList(tableName, deviceId, section);
        device.setList(list);
        return device;
    }

    public Page<ParticleValueHistoryVO> eqParticleValueHistoryList(Integer pageNum, Integer pageSize, String deviceId, int section) {
        //创建分页对象
        Page<ParticleValueHistoryVO> page = new Page<>(pageNum, pageSize);
        DeviceVO device = deviceListManager.deviceLocation(deviceId);
        Long productId = device.getProductId();
        String tableName = "properties_" + productId;
        pageNum=(pageNum-1)*pageSize;

        //查询粒子总数
        Integer count = deviceListManager.queryParticleValueHistoryCount(deviceId, tableName, section);

        //查询day05的粒子数历史记录
        List<ParticleValueHistoryVO> day05List = deviceListManager.eqParticleValueDay05History(pageNum, pageSize, deviceId, tableName, section);
        //查询day50的粒子数历史记录
        List<ParticleValueHistoryVO> day50List = deviceListManager.eqParticleValueDay50History(pageNum, pageSize, deviceId, tableName, section);
        //根据时间匹配
        // 创建一个新的列表来存储合并后的结果
        List<ParticleValueHistoryVO> resultList = new ArrayList<>();
        // 遍历 day05List
        for (ParticleValueHistoryVO vo1 : day05List) {
            // 遍历 day50List 寻找匹配的元素
            for (ParticleValueHistoryVO vo2 : day50List) {
                if (Objects.equal(vo1.getCreateTime(), vo2.getCreateTime())) {
                    vo1.setDay50(vo2.getDay50());
                    resultList.add(vo1); // 添加匹配的元素
                    break; // 找到匹配后跳出内层循环
                }
            }
        }

        // 如果需要，可以将 day50List 中未匹配的元素添加到结果列表中
        for (ParticleValueHistoryVO vo2 : day50List) {
            boolean matched = false;
            for (ParticleValueHistoryVO vo1 : day05List) {
                if (Objects.equal(vo1.getCreateTime(), vo2.getCreateTime())) {
                    matched = true;
                    break;
                }
            }
            if (!matched) {
                resultList.add(vo2);
            }
        }

        page.setRecords(day05List);
        page.setTotal(count); //设置粒子总数
        page.setRecords(day05List);
        return page;
    }
}
